package com.ruoyi.web.controller.bank;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TAppBalance;
import com.ruoyi.system.service.ITAppBalanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 余额Controller
 * 
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/balance")
public class TAppBalanceController extends BaseController
{
    @Autowired
    private ITAppBalanceService tAppBalanceService;

    /**
     * 查询余额列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TAppBalance tAppBalance)
    {
        startPage();
        List<TAppBalance> list = tAppBalanceService.selectTAppBalanceList(tAppBalance);
        return getDataTable(list);
    }

    /**
     * 导出余额列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, TAppBalance tAppBalance)
    {
        List<TAppBalance> list = tAppBalanceService.selectTAppBalanceList(tAppBalance);
        ExcelUtil<TAppBalance> util = new ExcelUtil<TAppBalance>(TAppBalance.class);
        util.exportExcel(response, list, "余额数据");
    }

    /**
     * 获取余额详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tAppBalanceService.selectTAppBalanceById(id));
    }

    /**
     * 新增余额
     */
    @PostMapping
    public AjaxResult add(@RequestBody TAppBalance tAppBalance)
    {
        return toAjax(tAppBalanceService.insertTAppBalance(tAppBalance));
    }

    /**
     * 修改余额
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TAppBalance tAppBalance)
    {
        return toAjax(tAppBalanceService.updateTAppBalance(tAppBalance));
    }

    /**
     * 删除余额
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tAppBalanceService.deleteTAppBalanceByIds(ids));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception{
        ExcelUtil<TAppBalance> util = new ExcelUtil<>(TAppBalance.class);
        List<TAppBalance> tAppBalances = util.importExcel(file.getInputStream());
        for (TAppBalance tAppBalance : tAppBalances) {
            if(StringUtils.isNotEmpty(tAppBalance.getDate())){
                tAppBalance.setDate(tAppBalance.getDate().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppBalance.getOpeningBal())){
                tAppBalance.setOpeningBal(tAppBalance.getOpeningBal().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppBalance.getClosingBal())){
                tAppBalance.setClosingBal(tAppBalance.getClosingBal().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppBalance.getDebitAmount())){
                tAppBalance.setDebitAmount(tAppBalance.getDebitAmount().trim().replace("\n", ""));
            }
            if(StringUtils.isNotEmpty(tAppBalance.getCreditAmount())){
                tAppBalance.setCreditAmount(tAppBalance.getCreditAmount().trim().replace("\n", ""));
            }
        }
        tAppBalanceService.importTAppBalances(tAppBalances);
        return AjaxResult.success(tAppBalances);
    }

}
